//
//  MsgCenterView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/19.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "MsgCenterView.h"
#import "MsgTaskCell.h"
#import "MsgFriendCell.h"

@interface MsgCenterView()<UICollectionViewDataSource, UICollectionViewDelegate, MsgFriendCellDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) MsgTaskCell *task_list_view;
@property (nonatomic, strong) MsgFriendCell *friend_list_view;

@end

@implementation MsgCenterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = RGBA(255, 255, 255, 1);
        self.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
        self.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
        self.layer.shadowOpacity = 0.3;
        self.layer.shadowRadius = 1;
        
        
        [self makeCollectionView];
    }
    return self;
}

- (void)makeCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH - 80, SCREEN_HEIGHT - 140);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.backgroundColor = CLEARCOLOR;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[MsgTaskCell class] forCellWithReuseIdentifier:NSStringFromClass([MsgTaskCell class])];
    [_collectionView registerClass:[MsgFriendCell class] forCellWithReuseIdentifier:NSStringFromClass([MsgFriendCell class])];
    [self addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
}

- (void)scrollCollectionViewToIndex:(NSInteger)index
{
    [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}

#pragma mark ```collectionview```datasource and delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    _friend_list_view = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MsgFriendCell class]) forIndexPath:indexPath];
    _friend_list_view.delegate = self;
    return _friend_list_view;
    
//    if (indexPath.row == 0) {
//        _task_list_view = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MsgTaskCell class]) forIndexPath:indexPath];
//        
//        return _task_list_view;
//    }else if (indexPath.row == 1){
//        _friend_list_view = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MsgFriendCell class]) forIndexPath:indexPath];
//        _friend_list_view.delegate = self;
//        return _friend_list_view;
//    }
//    return nil;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageWith = SCREEN_WIDTH - 80;

    int page = floor((scrollView.contentOffset.x - (pageWith) / 2) / (pageWith)) + 1;
    
    if ([self.delegate respondsToSelector:@selector(slideCenterViewWithPage:)]) {
        [self.delegate slideCenterViewWithPage:(NSInteger)page];
    }

}

- (void)friendHeaderWithRefreshingData
{
    if ([self.delegate respondsToSelector:@selector(friendListRefreshData)]) {
        [self.delegate friendListRefreshData];
    }
}

- (void)loadFriendListData:(NSArray *)data
{
    _friend_list_view.data = data;
}

- (void)didSelectAgreeIndexFriend:(FriendModel *)model
{
    if ([self.delegate respondsToSelector:@selector(didSelectAgreeWithFriend:)]) {
        [self.delegate didSelectAgreeWithFriend:model];
    }
}

@end
